<?php

namespace Home\Lib;

use Think\Controller;

class Discount extends Controller
{
    //添加优惠率
    public function add()
    {
        $userinfo = M('userinfo');
        $resUser = $userinfo->where(['part' => 'unit'])->select();
        $this->assign('resUser', $resUser);
        $this->assign('module_name', I('get.moduel_name'));
        $this->assign('class', I('get.class'));
        $this->assign('type', I('get.type'));
        $this->assign('mid', I('get.number'));
        $this->assign('modid', I('get.id'));
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->display('addDiscount');
    }

    public function addHandler()
    {
        if (IS_POST) {
            $discount = M('discount');
            $lsection = I('post.lsection') ? I('post.lsection') : 1;
            $rsection = I('post.rsection') ? I('post.rsection') : 10000000;
            $_POST['lsection'] = $lsection;
            $_POST['rsection'] = $rsection;
            if ($discount->add($_POST)) {
                $this->success('添加成功', U('Home/Module/discount', ['status' => 'info', 'id' => I('post.modid')]));
            } else {
                $this->success('添加失败');
            }
        }
    }

    // 优惠率信息
    public function info()
    {
        $module = M('module');
        $result = $module->where(['id' => I('get.id')])->find();
        $userData = M('userinfo')->select();
        $this->fpage('discount', 50, array('modid' => I('get.id')));
        $this->assign('result', $result);
        $this->assign('userData', $userData);
        $this->assign('form', session('form'));
        $this->assign('username', session('user_name'));
        $this->display('discountInfo');
    }
    // 修改优惠率
    public function mod()
    {
        $resDiscount = M('discount')->where(['id' => I('get.id')])->find();
        $this->assign('resDiscount', $resDiscount);
        $this->assign('form', session('form'));
        $this->assign('modid', I('get.modid'));
        $this->assign("type", I('get.type'));
        $this->assign('username', session('user_name'));
        $this->display('modDiscount');
    }

    public function modHandler()
    {
        if (IS_POST) {
            $discount = M('Discount');
            $lsection = I('post.lsection') ? I('post.lsection') : 1;
            $rsection = I('post.rsection') ? I('post.rsection') : 10000000;
            $_POST['lsection'] = $lsection;
            $_POST['rsection'] = $rsection;
            if ($discount->save($_POST)) {
                $this->success('修改成功', U('Home/Module/discount', ['status' => 'info', 'id' => I('post.modid')]));
            } else {
                $this->success('修改失败');
            }
        }
    }
    // 删除优惠率
    public function del()
    {
        $discount = M('Discount');
        if ($discount->delete(I('get.id'))) {
            $this->success('删除成功', U('Home/Module/discount', ['status' => 'info', 'id' => I('get.modid')]));
        } else {
            $this->success('删除失败');
        }
    }

    private function fpage($table, $listRows, $con, $group = '', $order = '')
    {
        $table = M($table);
        $count = $table->where($con)->count();
        $fpage = new \Think\Page($count, $listRows);
        $fpage->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;
              第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $fpage->setConfig('first', '首页');
        $fpage->setConfig('prev', '上一页');
        $fpage->setConfig('next', '下一页');
        $fpage->setConfig('last', '末页');
        $fpage->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
        $fpage->rollPage = 5;
        $pageResult = $table->where($con)->group($group)->order($order)
                                ->limit($fpage->firstRow.','.$fpage->listRows)
                                ->select();
        $fpage->lastSuffix = false;
        $show = $fpage->show();
        $this->assign('count', $count);
        $this->assign('listRows', $fpage->listRows);
        $this->assign('moduleData', $pageResult);
        $this->assign('show', $show);
    }
}
